/*
 * RBFTest_Upload.c
 *
 *  Created on: 2023年7月22日
 *      Author: mcealt_DS
 * upload
 * RFM -> HUB  CMD：0x04 节点实时事件上报   event
 * RFM -> HUB  CMD：0x05 节点周期心跳 需包含自身外设输入状态    heartbeat
 * RFM <- HUB  CMD：0x06 网关应答上报  ack_event
 * RFM <- HUB  CMD：0x07 网关应答心跳  ack_heartbeat
 */
#include "RBFUpload.h"
#include "app_log.h"
#include <string.h>

#define CONFIG_LOG_RBFTEST_UPLOAD // 模块日志宏定义,使能,模块输出日志

typedef enum
{
  RBFUPLOAD_STEP_RFM_TO_HUB_EVENT,
  RBFUPLOAD_STEP_HUB_TO_RFM_ACK_EVENT,
  RBFUPLOAD_STEP_EVENT_UOLOAD_FINISH,
  RBFUPLOAD_STEP_RFM_TO_HUB_HEARTBEAT,
  RBFUPLOAD_STEP_HUB_TO_RFM_ACK_HEARTBEAT,
  RBFUPLOAD_STEP_HEARTBEAT_UOLOAD_FINISH,
} RBFUpload_test_step_t;

static RBFUpload_test_step_t step = RBFUPLOAD_STEP_RFM_TO_HUB_EVENT;
static uint8_t packet[256];

void RBFUpload_test_LOG(void)
{
#ifdef CONFIG_LOG_RBFTEST_UPLOAD
  switch (step)
  {
  case RBFUPLOAD_STEP_RFM_TO_HUB_EVENT:
    DS_app_log_debug("RBFUPLOAD_STEP is RFM_TO_HUB_EVENT:\r\n");
    break;
  case RBFUPLOAD_STEP_HUB_TO_RFM_ACK_EVENT:
    DS_app_log_debug("RBFUPLOAD_STEP is HUB_TO_RFM_ACK_EVENT:\r\n");
    break;
  case RBFUPLOAD_STEP_EVENT_UOLOAD_FINISH:
    DS_app_log_debug("RBFUPLOAD_STEP is EVENT_UOLOAD_FINISH!\r\n");
    break;
  case RBFUPLOAD_STEP_RFM_TO_HUB_HEARTBEAT:
    DS_app_log_debug("RBFUPLOAD_STEP is RFM_TO_HUB_HEARTBEAT:\r\n");
    break;
  case RBFUPLOAD_STEP_HUB_TO_RFM_ACK_HEARTBEAT:
    DS_app_log_debug("RBFUPLOAD_STEP is HUB_TO_RFM_ACK_HEARTBEAT:\r\n");
    break;
  case RBFUPLOAD_STEP_HEARTBEAT_UOLOAD_FINISH:
    DS_app_log_debug("RBFUPLOAD_STEP is HEARTBEAT_UOLOAD_FINISH!\r\n");
    break;
  default:
    break;
  }
#endif
}
static uint8_t kayvalue_buf[] = {0xA1, 0xA2, 0xA3, 0xA4};
void RBFUpload_test(void)
{
  RBFUpload_test_LOG();
  switch (step)
  {
  case RBFUPLOAD_STEP_RFM_TO_HUB_EVENT:
  {
    RBFMacMessageNodeUploadEvent_t msg_rec;
    RBFMacMessageNodeUploadEvent_t msg = {
        // 测试msg
        .macAdd = 0x1234,
        .msgSeq = 0x02,
        .msgRetry = 0x03,
        .battV = 0x04,
        .rssi = 0x05,
        .temp = 0x06,
        .qrfR1 = 0x07,
        .qrfR2 = 0x08,
        .echASeq = 0x09,
        .keyValueSize = 4,
        .keyValue = kayvalue_buf,
    };
    memset(packet, 0, sizeof(packet));
    RBF_node_upload_event_payload_msgToPacket(msg, packet);
    RBF_node_upload_event_payload_packetToMsg(packet, &msg_rec);
    if ((memcmp(&msg_rec, &msg, sizeof(RBFMacMessageNodeUploadEvent_t) - sizeof(uint8_t *)) == 0) &&
        (memcmp(msg_rec.keyValue, msg.keyValue, (unsigned int)msg.keyValueSize) == 0))
      DS_app_log_debug("NodeUploadEvent check ok!\r\n");
    else
    {
      DS_app_log_error("NodeUploadEvent check error!\r\n");
    }
    step++;
    break;
  }
  case RBFUPLOAD_STEP_HUB_TO_RFM_ACK_EVENT:
  {
    RBFMacMessageGWAckEvent_t msg_rec;
    RBFMacMessageGWAckEvent_t msg = {
        // 测试msg
        .rssi = 0x01,
        .qrfH1 = 0x02,
        .qrfH2 = 0x02,
        .aseq = 0x03,
        .keyValueSize = 4,
        .keyValue = kayvalue_buf,
    };

    memset(packet, 0, sizeof(packet));
    RBF_gw_ack_event_payload_msgToPacket(msg, packet);
    RBF_gw_ack_event_payload_packetToMsg(packet, &msg_rec);
    if ((memcmp(&msg_rec, &msg, sizeof(RBFMacMessageGWAckEvent_t) - sizeof(uint8_t *)) == 0) &&
        (memcmp(msg_rec.keyValue, msg.keyValue, (unsigned int)msg.keyValueSize) == 0))
      DS_app_log_debug("GWAckEvent check ok!\r\n");
    else
      DS_app_log_error("GWAckEvent check error!\r\n");
    step++;
    break;
  }
  case RBFUPLOAD_STEP_EVENT_UOLOAD_FINISH:
  {
    step++;
    break;
  }

  case RBFUPLOAD_STEP_RFM_TO_HUB_HEARTBEAT:
  {
    RBFMacMessageNodeUploadHeartbeat_t msg_rec;
    RBFMacMessageNodeUploadHeartbeat_t msg = {
        // 测试msg
        .macAdd = 0x1234,
        .msgSeq = 0x02,
        .msgRetry = 0x03,
        .battV = 0x04,
        .rssi = 0x05,
        .temp = 0x06,
        .qrfR1 = 0x07,
        .qrfR2 = 0x08,
        .echASeq = 0x09,
        .keyValueSize = 4,
        .keyValue = kayvalue_buf,
    };
    memset(packet, 0, sizeof(packet));
    RBF_node_upload_heart_payload_msgToPacket(msg, packet);
    RBF_node_upload_heart_payload_packetToMsg(packet, &msg_rec);
    if ((memcmp(&msg_rec, &msg, sizeof(RBFMacMessageNodeUploadHeartbeat_t) - sizeof(uint8_t *)) == 0) &&
        (memcmp(msg_rec.keyValue, msg.keyValue, (unsigned int)msg.keyValueSize) == 0))
      DS_app_log_debug("NodeUploadHeartbeat check ok!\r\n");
    else
      DS_app_log_error("NodeUploadHeartbeat check error!\r\n");
    step++;
    break;
  }
  case RBFUPLOAD_STEP_HUB_TO_RFM_ACK_HEARTBEAT:
  {
    RBFMacMessageGWAckHeartbeat_t msg_rec;
    RBFMacMessageGWAckHeartbeat_t msg = {
        // 测试msg
        .rssi = 0x01,
        .qrfH1 = 0x02,
        .qrfH2 = 0x02,
        .aseq = 0x03,
        .keyValueSize = 4,
        .keyValue = kayvalue_buf,
    };
    memset(packet, 0, sizeof(packet));
    RBF_gw_ack_heart_payload_msgToPacket(msg, packet);
    RBF_gw_ack_heart_payload_packetToMsg(packet, &msg_rec);
    if ((memcmp(&msg_rec, &msg, sizeof(RBFMacMessageGWAckHeartbeat_t) - sizeof(uint8_t *)) == 0) &&
        (memcmp(msg_rec.keyValue, msg.keyValue, (unsigned int)msg.keyValueSize) == 0))
      DS_app_log_debug("GWAckHeartbeat check ok!\r\n");
    else
      DS_app_log_error("GWAckHeartbeat check error!\r\n");
    step++;
    break;
  }
  case RBFUPLOAD_STEP_HEARTBEAT_UOLOAD_FINISH:
  {
    step++;
    step = 0xff;
    break;
  }
  default:
    break;
  }
}
